package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.OrderAndShopDao;
import com.qst.crop.dao.OrderDao;
import com.qst.crop.entity.Order;
import com.qst.crop.entity.OrderDeleteParams;
import com.qst.crop.entity.OrderDiscuss;
import com.qst.crop.entity.SellPurchase;
import com.qst.crop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author spengda
 * @date 2024/3/815:22
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    @Autowired
    OrderDao orderDao;
    @Autowired
    private OrderAndShopDao orderAndShopDao;
    @Override
    public PageInfo<Order> findAll(Integer pageNum) {
        //PageHelper.startPage方法会将分页信息放入ThreadLocal中，但是 PageHelper 方法调用后紧跟查询方法，就是安全的，因为查询方法后清除了Threadlocal里的值。
        PageHelper.startPage(pageNum,30);
        List<Order> orders =  orderDao.findAll();
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        //调用dao进行查询
        return orderPageInfo;
    }

    /**
     * @Description : 查询卖出订单
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.SellPurchase>>
     */
    @Override
    public List<SellPurchase> findAllSells(String username) {
        return orderDao.findByName(username);
    }



    @Override
    public Order selectById(int id) {
        return orderDao.selectById(id);
    }

    @Override
    public PageInfo<Order> selectById(Integer id){
        PageHelper.startPage(1,30);
        List<Order> orders =  orderDao.selectById(id);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        //调用dao进行查询
        return orderPageInfo;
    }
    @Override
    public PageInfo<Order> search(String username,Integer pageNum,String type) {
        PageHelper.startPage(pageNum,30);
        List<Order> orders=orderDao.search(username,type);
        PageInfo<Order> orderPageInfo=new PageInfo<>(orders);
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> searchAll(Integer pageNum,String type){
        PageHelper.startPage(pageNum,30);
        List<Order> orders=orderDao.searchAll(type);
        PageInfo<Order> orderPageInfo=new PageInfo<>(orders);
        return orderPageInfo;
    }

    /**
     * 查询全部需求
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Order> findAllNeeds(Integer pageNum){
        PageHelper.startPage(pageNum,30);
        List<Order> orders =  orderDao.findAllNeeds();
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        //调用dao进行查询
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> searchNeedsByKeys(String keys,Integer pageNum){
        PageHelper.startPage(pageNum,30);
        List<Order> orders =  orderDao.searchNeedsByKeys(keys);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        //调用dao进行查询
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> searchMyGoodsByKeys(Integer pageNum,String title) {
        PageHelper.startPage(pageNum,30);
        List<Order> orders=orderDao.searchMyGoodsByKeys(title);
        PageInfo<Order> orderPageInfo=new PageInfo<>(orders);
        return orderPageInfo;
    }

    @Override
    public int updateGoods(Order order) {
        return orderDao.updateGoods(order);
    }

    @Override
    public int insert(Order order) {
        return orderDao.insert(order);
    }




    @Override
    public PageInfo<Order> findAll(Integer pageNum,String username) {
        //PageHelper.startPage方法会将分页信息放入ThreadLocal中，但是 PageHelper 方法调用后紧跟查询方法，就是安全的，因为查询方法后清除了Threadlocal里的值。
        PageHelper.startPage(pageNum,30);
        List<Order> orders =  orderDao.findAll(username);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        //调用dao进行查询
        return orderPageInfo;
    }


    @Override
    public int insertSelective(Order record) {
        return orderDao.insertSelective(record);
    }
    @Override
    public Order selectByPrimaryKey(Integer id) {
        return orderDao.selectByPrimaryKey(id);
    }
    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderDao.updateByPrimaryKeySelective(record);
    }
    @Override
    public int updateByPrimaryKey(Order order) {

        return orderDao.updateByPrimaryKey(order);
    }
    @Override
    public Order selectBuyByUserName(String username){
        return orderDao.selectBuyByUserName(username);
    }
    @Override
    public Order selectSellByUserName(String username){

        return orderDao.selectSellByUserName(username);
    }
    @Override
    public Order selectOrderByUsernameAndType(String username,String type){
        return orderDao.selectOrderByUsernameAndType(username,type);
    }




    @Override
    public List<Order> selectBuys(String username) {
        return orderDao.selectBuys(username);
    }




    @Override
    public PageInfo<Order> searchMyNeedsByKeys(Integer pageNum,String content,String username) {
        PageHelper.startPage(pageNum,5);
        List<Order> orderPageInfo = orderDao.searchMyNeedsByKeys(content, username);
//        log.info("getorder"+orderPageInfo);
        PageInfo<Order> orderPageInfo1 = new PageInfo<>(orderPageInfo);
//        log.info("getorder"+orderPageInfo1);
        return orderPageInfo1;
    }

    @Override
    public int deleteByPrimaryKey(OrderDeleteParams params){
        return orderDao.deleteByPrimaryKey(params);
    }

    @Override
    public PageInfo<Order> searchAllByKeys(String keys,Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Order> orderPageInfo = orderDao.searchAllByKeys(keys);
        PageInfo<Order> orderPageInfo1 = new PageInfo<>(orderPageInfo);
        return orderPageInfo1;
    }

    @Override
    public int delOrderById(Integer id){
        return orderDao.delOrderById(id);
    }

    @Override
    public Order getById(Integer orderId) {
        return orderDao.selectByPrimaryKey(orderId);
    }

    @Override
    public PageInfo<Order> findAllGoods(Integer pageNum){
        PageHelper.startPage(pageNum,30);
        List<Order> orderPageInfo = orderDao.findAllGoods();
        PageInfo<Order> orderPageInfo1 = new PageInfo<>(orderPageInfo);
        return orderPageInfo1;
    }

    @Override
    public PageInfo<Order> searchGoodsByKeys(String keys, Integer pageNum){
        PageHelper.startPage(pageNum,30);
        List<Order> orderPageInfo = orderDao.searchGoodsByKeys(keys);
        PageInfo<Order> orderPageInfo1 = new PageInfo<>(orderPageInfo);
        return orderPageInfo1;
    }

    /**
     * 上架商品
     * @param id
     * @return
     */
    @Override
    public int takeUpDownOrder(Integer id,Integer status){
        return orderDao.takeUpDownOrder(id,status);
    }

    @Override
    public List<String> getOrderCarousel(Integer id){
        return orderDao.getOrderCarousel(id);
    }

    @Override
    public int leaveComments(OrderDiscuss orderDiscuss){
        return orderDao.leaveComments(orderDiscuss);
    }

    @Override
    public int commentsNumAdd(String username,Integer newNum){
        return orderDao.commentsNumAdd(username,newNum);
    }
}
